﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Net;

namespace WindOPC
{
    class Bom
    {
        public Bom()
        {
        }

        /// <summary>
        /// Get HTML code from the provided url then return as a string.
        /// </summary>
        public string getHTMLPage(string url)
        {
            string html = "";
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse respond = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(respond.GetResponseStream());
                html = reader.ReadToEnd();
                reader.Close();

            }
            catch (Exception ex)
            { 
            }
            return html;
        }

        /// <summary>
        /// Remove invalid XML characters from the given string.
        /// The function will remove the first "/" from the string
        /// </summary>
        public string RemoveInvalidXmlChars(string str)
        {
            int i = str.IndexOf("/");
            return str.Remove(i, 1);
        }

        /// <summary>
        /// Extract wind speed and direction from the given HTML code 
        /// the return data as a string array. 
        /// </summary>
        public string[] getWindData(string html)
        {
            string[] strArr = new string[2];
            if (html != "")
            {
                // Split the HTML code into parts by row name (separator) of the wind data table in the HTML code.
                string[] rows = html.Split(new string[] { "rowleftcolumn" }, StringSplitOptions.None);
                // Get the most update row then remove comment part from the code
                string[] firstRow = rows[1].Split(new string[] { "<!--" }, StringSplitOptions.None);
                // Add "<tr" to the string firstRow[0]
                string testStr = "<tr" + firstRow[0].Remove(0, 1);
                // Remove invalid XML charcter to make the string as a XML document
                string wellXml = RemoveInvalidXmlChars(testStr);
                XmlDocument xml = new XmlDocument();
                xml.LoadXml(wellXml);
                // Make a list of <td> node
                XmlNodeList xnList = xml.SelectNodes("tr/td");
                // Now it is easy to get wind data
                foreach (XmlNode xn in xnList)
                {
                    if (xn != null)
                    {
                        //direction
                        if (xn.Attributes["headers"].Value == "t1-wind t1-wind-dir")
                        {
                            strArr[1] = xn.InnerText;
                        }
                        //speed
                        if (xn.Attributes["headers"].Value == "t1-wind t1-wind-spd-kmh")
                        {
                            strArr[0] = xn.InnerText;
                        }                 
                    }
                }
            }
            return strArr;
        }
    }
}








